package com.sunchangming.stmdb.xgen;

public class TypeFloat extends Type {

	@Override
	public String getName(Variable variable) {
		return "float";
	}

	@Override
	public String getMarshalStr(Variable v, String osname) {
		return String.format("%s.writeFloat(%s)", osname, v.getName());
	}

	@Override
	public String getUnmarshalStr(Variable v, String osname, boolean useThis) {
		return String.format((useThis ? "this." : "") + "%s=%s.readFloat()", v.getName(), osname);
	}

	@Override
	public String getBoxName() {
		return "Float";
	}

	@Override
	public String getDefaultValue(Variable variable) {
		final String userstr = variable.defaultvalue;
		if (userstr != null && !userstr.isEmpty()) {
			try {
				return String.valueOf(Float.valueOf(userstr) + "f");
			} catch (final NumberFormatException ex) {
				throw new RuntimeException("错误的默认值" + userstr, ex);
			}
		}
		return "0f";
	}

	@Override
	public String getTestEqualsStr(Variable variable, String string) {
		return "this." + variable.getName() + "== " + string + "." + variable.getName();
	}

	@Override
	public String getHashCodeStr(Variable variable) {
		return String.format("Float.floatToIntBits(this.%s)", variable.getName());
	}

}
